package com.example.compose

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.lifecycle.Lifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.dialog
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument

@SuppressLint("CompositionLocalNaming")
var ActiveUser = compositionLocalOf<Context> { error("No active user found!") }

class MainActivity2 : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.R)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val decorView = window.decorView
        val controller = decorView.windowInsetsController
        setContent {
            CompositionLocalProvider(ActiveUser provides baseContext) {
//                AppView(10f)
            }
        }

    }
}

@Composable
fun AppView(modifier: Modifier) {
    val  navController: NavHostController =  rememberNavController()
    Scaffold {
        NavHost(navController, Screen.Profile.route) {
            composable(Screen.Profile.route) { backStackEntry -> ProfileView(navController,backStackEntry) }
            composable(Screen.Dashboard.route) { backStackEntry -> DashboardView(navController,backStackEntry) }
            composable(Screen.Scrollable.route) { backStackEntry -> ScrollableView(navController,backStackEntry) }
            composable(Screen.PageJump.route) { backStackEntry -> PageJumpView(navController,backStackEntry) }
            dialog(Screen.Dialog.route, arguments = listOf(navArgument("title") { type = NavType.StringType })) { backStackEntry -> DialogContent(navController,backStackEntry) }
        }
    }
}
@Composable
fun DialogContent(navController: NavHostController,backStackEntry: NavBackStackEntry) {
    AlertDialog(
        onDismissRequest = {},
        title = { Text(text = backStackEntry.arguments?.getString("title","")?: "")},
        confirmButton = {
            TextButton(onClick = {
                navController.popBackStack()
            }) {
                Icon(Icons.Filled.Done, contentDescription = null )
            }
        }
    )
}

sealed class Screen(val route: String) {
    object Profile : Screen("profile")
    object Dashboard : Screen("dashboard")
    object Scrollable : Screen("scrollable")
    object PageJump : Screen("page_jump")
    object Dialog : Screen("dialog/{title}") {
        fun createRoute(title: String) = "dialog/$title"
    }
}

@Composable
fun ScrollableView(navController: NavHostController,backStackEntry: NavBackStackEntry) {
    Column(Modifier.fillMaxSize()) {
        OutlinedButton(onClick = {
            //防止重复调用
            if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dialog.createRoute(Uri.encode("这是另一个提示")))
        }) {
            Text(text = "这是另一个提示")
        }
        OutlinedButton(onClick = {
            navController.popBackStack()
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to Back")
        }
    }
}

@Composable
fun DashboardView(navController: NavHostController,backStackEntry: NavBackStackEntry) {
    Column(Modifier.fillMaxSize()) {
        OutlinedButton(onClick = {
            //防止重复调用
            if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dialog.createRoute(Uri.encode("这是一个提示")))
        }) {
            Text(text = "这是一个提示")
        }
        OutlinedButton(onClick = {
            navController.popBackStack()
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to Back")
        }
    }
}

@Composable
fun PageJumpView(navController: NavHostController, backStackEntry: NavBackStackEntry) {
    var text by rememberSaveable { mutableStateOf("") }
    var isjavaScript by remember { mutableStateOf(false) }
    val context = App().getContext()
    Column(Modifier.fillMaxSize()) {
        OutlinedTextField(
            modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight(),
            value = text,
            onValueChange = { text = it },
            label = { Text("url") }
        )
        Checkbox(checked = isjavaScript, onCheckedChange = {
            isjavaScript = it
        }, Modifier.align(Alignment.CenterHorizontally))

        OutlinedButton(onClick = {
            navController.popBackStack()
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to Back")
        }
        OutlinedButton(onClick = {
            val intent = Intent(context , MainActivity3::class.java)
            if (text.isNotEmpty()) intent.putExtra("url",text)
            intent.putExtra("javaScript",isjavaScript)
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            context.startActivity(intent)
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to MainActivity3")
        }
    }
}

@Composable
fun ProfileView(navController: NavHostController,backStackEntry: NavBackStackEntry) {
    Column(Modifier.fillMaxSize()) {
        OutlinedButton(onClick = {
            //防止重复调用
            if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dialog.createRoute(Uri.encode("这是一个警告")))
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "这是一个警告")
        }
        OutlinedButton(onClick = {
            //防止重复调用
            if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Dashboard.route)
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to DashboardView")
        }
        OutlinedButton(onClick = {
            //防止重复调用
            if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.Scrollable.route)
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to ScrollableView")
        }
        OutlinedButton(onClick = {
            //防止重复调用
            if(backStackEntry.lifecycle.currentState == Lifecycle.State.RESUMED) navController.navigate(Screen.PageJump.route)
        }, Modifier.align(Alignment.CenterHorizontally)) {
            Text(text = "to PageJump")
        }
    }
}

